#include"p2p.h"
static int flag_first = 0;
void writeconf(FILE *conf, struct tasklist *taskl[], struct hostlist *hostl[])
{
	// the file has been opened
/*	fputs("#tasklist ", conf);
	fputs('\n', conf);
	for(int i = 0; i < 10; i++)
	{
	fputs(taskl[i]->task, conf);
	fputs(':', conf);
	fputs(taskl[i]->flag, conf);
	fputs(':', conf);
	fputs(taskl[i]->host, conf);
	fputs(':', conf);
	fputs(taskl[i]->pfseek, conf);
	fputs(':', conf);
	fputs(taskl[i]->size, conf);
	fputs('\n', conf);
	}
	
	fputs("#hostlist ", conf);
	fputs('\n', conf);
	for(int i = 0; i < 20; i++)
	{
	fputs(hostl[i]->host, conf);
	fputs(':', conf);
	fputs(hostl[i]->task, conf);
	fputs(':', conf);
	fputs(hostl[i]->on, conf);
	fputs(':', conf);
	fputs(hostl[i]->hfseek, conf);
	fputs(':', conf);
	fputs(hostl[i]->size, conf);
	fputs('\n', conf);
	}

	*/
	int i;
	i = fwrite(taskl[0], sizeof(struct tasklist), TASK, conf);
	if(i != TASK)
	{
		perror("fwrite task error");
		exit(1);
	}
	i = fwrite(hostl[0], sizeof(struct hostlist), 20, conf);
	if(i != 20)
	{
		perror("fwrite host error");
		exit(1);
	}
	 printf("write finished\n");
	 
	 
	fclose(conf);

}


void readconf(FILE *conf, struct tasklist *taskl[], struct hostlist *hostl[])
{
	char line[1024];
	int len;
	char *p;

	printf("check conf ...\n");
/*	while(fgets(line, sizeof(line), conf) != NULL)
	{
		if( (line[0] == '#') || (line[0] == '\n'))
			continue;
		len = strlen(line);
		i	i = fwrite(taskl[0], sizeof(struct tasklist), 1, conf);
	if(i != 10)
	{
		perror("fwrite error");
		exit(1);
	}f(line[len - 1] == '\n')
			line[len - 1] = '\0';   // form to a string
		if(( (p = strchr(line, ':')) == NULL) || (p == line) )  //if not find :
			continue;
		*p = '\0';
		p++;
	}
		
*/

	int i;
	i = fread(taskl[0], sizeof(struct tasklist), TASK, conf);
	if(i < TASK)
	{
		perror("fread task error");
		exit(1);
	}
	fseek(conf, 0, SEEK_CUR);   //当前位置	
	i = fread(hostl[0], sizeof(struct hostlist), 20, conf);
	if(i < 20)
	{
		perror("fread host error");
		exit(1);
	}
//======================================================
	printf("task size is %d\n", taskl[0]->size);
	printf("task size is %d\n", taskl[1]->size);
//======================================================
	

}


FILE *conf;
void initList(struct tasklist *taskl[], int listlen, struct hostlist *hostl[], int hostlen, char *urlBuff, int bufflen)
{
	int tottasklen;
	int tothostlen;
	int fp, i;
	

/*	tottasklen = sizeof(taskl);
	tothostlen = sizeof(hostl);
	memset(taskl, 0, tottasklen);
	memset(hostl, 0, tothostlen);
*/
	printf("%s\n", urlBuff);
	if((fp = open("conf", O_RDWR)) == -1)
	{
		perror(".conf do not exist");// p2p.conf do not exist
		fp = creat("conf", 0666);//creat p2p.conf
		if(fp < 0)
		{
		
			perror("creat file error");
			exit(1);
		}	
		conf = fopen("conf", "r+");
		//writeconf(conf, taskl, hostl);
	}else
	{	
		//printf("conf file exist\n");
		i = close(fp);
		if(i < 0)
		{
			perror("close error");
			exit(1);
		}
		conf = fopen("conf", "r+");
		if(conf == NULL)
		{
			perror("conf file open error");
			exit(1);
		}
		printf("begin readconf\n");
		readconf(conf, taskl, hostl);

	}
	

}
int just[TASK] = {0};
void sigWriteConf(void)
{
	int i;
	extern FILE *conf;
	extern struct hostlist *hostl[20];
	extern struct tasklist *taskl[TASK];
	for(i = 0; i < TASK; i++)
	{taskl[i]->pfseek = just[i];}
	writeconf(conf, taskl, hostl);
	exit(0);
}

struct hostlist *hostl[20];
struct tasklist *taskl[TASK];
int main(int argc, char **argv)
{
	
	pthread_t tid, tid2[TASK];
	int i, j, reqlen, size;
	char urlBuff[1024];
	char *pp[10], *p;
	int count_pp;
	char urlBuff2[1024];
	int val_pthread[TASK];

	printf("please input your url request:\n");
	scanf("%s", urlBuff);

	reqlen = strlen(urlBuff);
	urlBuff[reqlen] = '\0';
	strcpy(urlBuff2, urlBuff);
	urlBuff2[reqlen] = '\0';
	i = 0;
	pp[0] = urlBuff;
	p = urlBuff;
	while(p)
	{
		
		p = strchr(pp[i], '/');
		if(p == NULL)
			break;
		
		*p = '\0';
		i++;
		pp[i] = (++p);
	}
	count_pp = i;
	//=================================================================
	printf("the len of url %d \n", reqlen);
	for(i = 0; i <= count_pp; i++)
		{printf("req  %s\n", pp[i]);
		}
	//=================================================================
	for(i = 0; i < TASK; i++)
	{
		taskl[i] = (struct tasklist *)calloc(1, sizeof(struct tasklist));
		taskl[i]->task = i+1;
	}
	for(i = 0; i < 20; i++)
	{
		hostl[i] = (struct hostlist *)calloc(1, sizeof(struct hostlist));
		hostl[i]->hfseek = 0;
	}
	
	//==========================================================================================
	taskl[0]->size = 1304;
	taskl[1]->size = 1314;
	printf("the length of tasklist is %d,%d; hostlist length %d,%d \n", sizeof(struct tasklist),sizeof(taskl), sizeof(struct hostlist), sizeof(hostl));
	//==========================================================================================
	initList(taskl, TASK, hostl, 20, pp[count_pp], reqlen);

	sigset(SIGINT, sigWriteConf); 
//====================================================================================================断点续传
	if(flag_first == 0)
	{

		continueDown(urlBuff2, reqlen);
		flag_first = 1;
	}




//===========================================================================================================

	
	/*  updataHostlist();*/
	i = pthread_create(&tid, NULL, updataHostlist, NULL);
	if (i != 0)
	{
		perror("pthread_create error");
		exit(2);
	}








	for(j = 0; j < TASK; j++)
	{
		val_pthread[j] = j;
		i = pthread_create(&tid2[j], NULL, recvData, (void *)&val_pthread[j]);
		if (i != 0)
		{
			perror("pthread_create error");
			exit(2);
		}		
	}
	size = getReqSize(pp,count_pp);
	distriTask(urlBuff2, reqlen, pp[count_pp]);
	/*  recvData*/
	
	//signal(ALARM, updatehostlist2);  心跳服务
	pthread_join(tid, NULL);  //等待updataHostlist线程结束
	for(j = 0; j < TASK; j++)
	{pthread_join(tid2[j], NULL);}  //等待   XXXXXXX线程结束
	return 0;
}
